<?php

namespace App\Admin\Controllers;

use App\Admin\Components\SettingForm;
use App\Constant\Enum;
use App\Http\Controllers\Controller;
use App\Models\Setting;
use App\Models\UserWallet;
use Dcat\Admin\Layout\Content;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use App\Models\User;
use App\Models\UserWithdraw;
use App\Models\Adreward;
use Dcat\Admin\Layout\Row;
use App\Admin\Metrics\Examples\UserWalletsDevices;
use App\Admin\Metrics\Examples\UserblockedWalletsDevices;
class WithdrawController extends Controller{

    public function index(Content $content,Request $request)
    {
        // 获取筛选条件：起止时间
        $startDate = $request->input('start_date');
        $endDate = $request->input('end_date');

        $totals = UserWallet::selectRaw('ROUND(SUM(reward_coins) / 10000, 2) as reward_sum, ROUND(SUM(commission_coins) / 10000, 2) as commission_sum')->first();

        $withdrawUndoCommissionCoinsSum = $totals->commission_sum;
        $withdrawUndoRewardCoinsSum = $totals->reward_sum;

        $query = UserWithdraw::query();

        if ($startDate && $endDate) {

            if ($startDate == $endDate) {

                $query->whereDate('created_at', $startDate);
            } else {

                $query->whereBetween('created_at', [$startDate, $endDate]);
            }
        }

        $withdrawWaitCommissionCoinsSum = $query->clone()->where('wallet_type',Enum::WALLET_TYPE_COMMISSION)->where('status','pending')->sum('money');
        $withdrawWaitRewardCoinsSum = $query->clone()->where('wallet_type',Enum::WALLET_TYPE_REWARD)->where('status','pending')->sum('money');
        $withdrawSuccessCommissionCoinsSum = $query->clone()->where('wallet_type',Enum::WALLET_TYPE_COMMISSION)->where('status','approved')->sum('money');
        $withdrawSuccessRewardCoinsSum = $query->clone()->where('wallet_type',Enum::WALLET_TYPE_REWARD)->where('status','approved')->sum('money');
        $data = [
            "withdrawUndo"=>[
                "commission"=>$withdrawUndoCommissionCoinsSum,
                "reward"=>$withdrawUndoRewardCoinsSum,
            ],
            "withdrawWait"=>[
                "commission"=>$withdrawWaitCommissionCoinsSum,
                "reward"=>$withdrawWaitRewardCoinsSum
            ],
            "withdrawSuccess"=>[
                "commission"=>$withdrawSuccessCommissionCoinsSum,
                "reward"=>$withdrawSuccessRewardCoinsSum
            ]
        ];
        return $content
            ->title('数据展示')
            ->description('数据展示')
          //  ->body(view('withdraw', compact('data')))
            ->body(function (Row $row) {

                $row->column(4, new UserWalletsDevices());
                $row->column(4, new UserblockedWalletsDevices());
            });
    }
}
